<template>
  <a-modal
    title="新增費用類別"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="押金名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入押金名称" v-decorator="['depositName', {rules: [{required: true, message: '请输入押金名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="计数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group  placeholder="请选择计数" v-decorator="['count',{rules: [{ required: true, message: '请选择计数' }]}]" >
            <a-radio v-for="(item,index) in countData" :key="index" :value="item.code">{{ item.name }}</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { busCostBillDepositAdd } from '@/api/modular/main/buscostbilldeposit/busCostBillDepositManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        countData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    created () {
      this.selectedByDefault()
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        const countOption = this.$options
        this.countData = countOption.filters['dictData']('rember_count')
      },
      /**
       * 默认选中项
       */
      selectedByDefault () {
        this.form.getFieldDecorator('count', { valuePropName: 'checked', initialValue: '0' })
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            busCostBillDepositAdd(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.msg)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
